home *** CD-ROM | disk | FTP | other *** search
- #
- # This file is part of OpenVIP (http://openvip.sourceforge.net)
- #
- # Copyright (C) 2002-2003
- # Michal Dvorak, Jiri Sedlar, Antonin Slavik, Vaclav Slavik, Jozef Smizansky
- #
- # This program is licensed under GNU General Public License version 2;
- # see file COPYING in the top level directory for details.
- #
- # $Id: openvip.py,v 1.20 2003/05/25 09:28:36 vaclavslavik Exp $
- #
- # Highlevel Python interface to OpenVIP
- #
-
- import openvip_c
- openvip_c.InitExtension()
-
- VERSION = openvip_c.OPENVIP_VERSION
-
- PLUGIN_VIDEO_FILTER = openvip_c.OPENVIP_PLUGIN_VIDEO_FILTER
- PLUGIN_AUDIO_FILTER = openvip_c.OPENVIP_PLUGIN_AUDIO_FILTER
- PLUGIN_VIDEO_TRANSITION = openvip_c.OPENVIP_PLUGIN_VIDEO_TRANSITION
- PLUGIN_AUDIO_TRANSITION = openvip_c.OPENVIP_PLUGIN_AUDIO_TRANSITION
- PLUGIN_INPUT = openvip_c.OPENVIP_PLUGIN_INPUT
- PLUGIN_OUTPUT = openvip_c.OPENVIP_PLUGIN_OUTPUT
-
-
- class Error(Exception):
- def __init__(self, msg):
- self.msg = msg
- def __str__(self):
- return self.msg
-
-
- class UICallback:
- def show_gauge(self): pass
- def update_gauge(self, value): return 1
- def hide_gauge(self): pass
- def set_gauge_text(self, msg): pass
- def log_info(self, msg): pass
- def log_error(self, msg): pass
- def log_warning(self, msg): pass
-
- NullUICallback = UICallback()
-
-
- class PluginInfo:
- def __init__(self, obj):
- self.name = obj.name
- self.description = obj.description
-
- class VideoStreamInfo:
- def __init__(self, i):
- self.name = i.name
- self.width = i.width
- self.height = i.height
- self.fps = i.fps
- self.aspect = i.aspect
- self.length = i.length
-
- class AudioStreamInfo:
- def __init__(self, i):
- self.name = i.name
- self.sample_rate = i.sample_rate
- self.length = i.length
- self.channels = i.channels
-
- class FileInfo:
- def __init__(self, fi_c):
- self.filename = fi_c.filename
- self.video_streams = []
- self.audio_streams = []
- for i in range(0, fi_c.video_cnt):
- x = openvip_c.openvip_video_stream_info_t_array_getitem(
- fi_c.video_streams, i)
- self.video_streams.append(VideoStreamInfo(x))
- for i in range(0, fi_c.audio_cnt):
- x = openvip_c.openvip_audio_stream_info_t_array_getitem(
- fi_c.audio_streams, i)
- self.audio_streams.append(AudioStreamInfo(x))
-
- def get_stream(self, name):
- """Return object describing given stream or None if not present."""
- for s in self.video_streams:
- if s.name == name: return s
- for s in self.audio_streams:
- if s.name == name: return s
- return None
-
-
- class DestCallback:
- def __init__(self, callback_func):
- self.func = callback_func
- def set_dim(self, w, h):
- self.width = w
- self.height = h
- def add(self, data):
- self.func(data, self.width, self.height)
-
- class DestCallbackPIL(DestCallback):
- """Callback object that converts data to Python Imaging Library object."""
- def __init__(self, callback_func):
- import Image
- self.func = callback_func
- def add(self, data):
- import Image
- img = Image.fromstring("RGB", (self.width, self.height), data)
- self.func(img)
-
- class Task:
- def __init__(self, lib, task, dest=None):
- self.lib = lib
- self.ctask = task
- self.dest = dest
- def __del__(self):
- openvip_c.openvip_destroy_task(self.lib.clib, self.ctask)
-
- def render(self, callback=None):
- if openvip_c.openvipRender(self.lib.clib, self.ctask, callback) == 0:
- raise Error('rendering failed')
-
- def render_single_frame(self, frame):
- return openvip_c.RenderSingleFrame(self.lib.clib, self.ctask,
- frame,
- self.width*self.height*3)
-
-
- class Library:
- def __init__(self, path = None):
- if path == None:
- self.clib = openvip_c.openvip_create_with_defaults()
- else:
- self.clib = openvip_c.openvip_create(path)
- def __del__(self):
- openvip_c.openvip_destroy(self.clib)
-
- def __make_dest_t(self, width, height, dest):
- d = openvip_c.openvip_dest_t()
- if isinstance(dest, DestCallback):
- dest.set_dim(width, height)
- openvip_c.SetDestCallback(d, dest)
- else:
- raise TypeError('dest must derive from openvip.DestFile or openvip.DestCallback')
- return d
-
-
- def load_network_file(self, filename):
- t = openvip_c.openvip_load_network_file(self.clib, filename)
- if t == None:
- raise Error("failed to load file '%s'" % filename)
- return Task(self, t)
-
-
- def load_network_from_string(self, xmldata):
- t = openvip_c.openvip_load_network_from_string(self.clib, xmldata)
- if t == None:
- raise Error("failed to load XML data:\n%s" % xmldata)
- return Task(self, t)
-
-
- def load_network_with_memoutput_from_string(self, xmldata, outputid,
- width, height, dest):
- d = self.__make_dest_t(width, height, dest)
- t = openvip_c.openvip_load_network_with_memoutput_from_string(self.clib, xmldata, outputid, d)
- if t == None:
- raise Error("failed to load XML data:\n%s" % xmldata)
- task = Task(self, t, dest)
- task.width = width
- task.height = height
- return task
-
- def create_thumbnails_generator(self,file,stream,width,height,frames,dest):
- d = self.__make_dest_t(width, height, dest)
- t = openvip_c.openvip_create_thumbnails_generator(self.clib,
- file, stream, width, height, frames, d)
- if t == None:
- raise Error("failed to load file '%s'" % file)
- task = Task(self, t, dest)
- task.width = width
- task.height = height
- return task
-
-
- def enum_plugins(self, type):
- pllist = openvip_c.openvip_enum_plugins(self.clib, type)
- outlist = []
- for i in range(0, pllist.cnt):
- outlist.append(PluginInfo(
- openvip_c.openvip_plugin_info_t_array_getitem(
- pllist.plugins, i)))
- openvip_c.openvip_free(self.clib, pllist)
- return outlist
-
-
- def get_file_info(self, filename):
- fi_c = openvip_c.openvip_get_file_info(self.clib, filename)
- if fi_c == None:
- raise Error("cannot get information about file '%s'" % filename)
- fi = FileInfo(fi_c)
- openvip_c.openvip_free(self.clib, fi_c)
- return fi
-
-
- def get_task_file_info(self, task, module):
- fi_c = openvip_c.openvip_get_task_file_info(self.clib,
- task.ctask, module)
- if fi_c == None:
- raise Error("cannot get information about network")
- fi = FileInfo(fi_c)
- openvip_c.openvip_free(self.clib, fi_c)
- return fi
-
-
- def set_ui_callback(self, callback):
- # verify it is valid UICallback object (C code wouldn't work otherwise):
- if not isinstance(callback, UICallback):
- raise TypeError('callback object must derive from openvip.UICallback')
- # remember callback object (this is needed because SetUICallback won't
- # increase reference count!):
- self.callback = callback
- # finally, set the callback:
- openvip_c.SetUICallback(self.clib, callback)
-
-